home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17848 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  103 lines

  1. Path: news.ycc.yale.edu!ackerber
  2. From: ackerber@econ.yale.edu (Daniel Ackerberg)
  3. Newsgroups: comp.lang.c,comp.lang.c++
  4. Subject: Help!! Allocating Memory
  5. Date: 17 Apr 1996 19:53:46 GMT
  6. Organization: Yale University
  7. Message-ID: <4l3i8a$6lu@news.ycc.yale.edu>
  8. NNTP-Posting-Host: dido.econ.yale.edu
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. Hi.  I am a novice trying to run a C program with a large (~10 Meg) array 
  12. on a Sparc 20 running SunOS and with 64 Meg of RAM.
  13.  
  14. The program essential calls a short  (~1.5 second) function that runs 
  15. through and performs some operations on the 10 Meg array.
  16.  
  17. My problem is that on starting the program, the "function" takes about 
  18. 1.5 seconds for each evaluation.  This continues for about 5-10 minutes. 
  19. Then, suddenly, after what seems to be a random amount of time or number 
  20. of evaluations, the function starts to take about 2.7 seconds per evaluation.
  21.  
  22. The following is the simplest program I have that exhibits this "bug":
  23.  
  24.  
  25. #include <math.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <time.h>
  29.  
  30. #define MEM 10      /* How many megs the array should be */
  31.  
  32. void main()
  33. {
  34. double *x;
  35. long int sz,i,k,c;
  36.  
  37. sz=MEM*1024000/8;
  38.  
  39. if ((x = (double *) malloc (sizeof(double) * sz)) == NULL) {
  40.    printf("Memory Error.\n");
  41.    exit(1);
  42. }
  43.  
  44. /* Loop over function Evaluations */
  45.  
  46. for(k=0;k<100000;k++)
  47. {
  48.    c=clock();
  49.    for(i=0;i<sz;i++) x[i]=0.99;
  50.    for(i=0;i<sz;i++) x[i]=exp(x[i]*x[i]);
  51.    printf("Time: %f\n",((float)(clock()-c))*1e-6); fflush(stdout);
  52. }
  53.  
  54. }
  55.  
  56.  
  57. Note that in this simple program, the function does the EXACT same thing 
  58. each time, so I do not understand why it starts off taking 1.5 seconds 
  59. and then switches to almost double the time.  I think it must have something 
  60. to do with the large size of the array and swapping , although I don't 
  61. understand why it would be swapping.  Also when I use "top" to watch what 
  62. is going on, the program starts with about 10 Meg resident in memory.  
  63. Then around the time it slows down, I occasionally see the resident memory 
  64. go down by 8 K.  Is this small amount of memory being swapped and 
  65. drastically slowing down the program?  If so , is there any way to prevent 
  66. this from happening?   I've showed this program to my system administrators 
  67. and they don't seem to know what is going on.  Any help would be 
  68. tremendously appreciated.  
  69.  
  70. Thank you ,  Dan.
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101. A
  102.  
  103.